package vsm.vsm_server.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.*;
import vsm.vsm_server.pojo.Video.Video;
import vsm.vsm_server.pojo.Video.VideoIdVO;
import vsm.vsm_server.pojo.Video.VideoTag;

import java.math.BigInteger;
import java.util.List;

@Mapper
public interface VideoMapper {
    @Select("select * from video where userid = #{userid} and isdelete = false order by createTime desc")
    List<Video> getSelfVideos(BigInteger userid);
    @Select("select videoId from video where userid = #{userid} and isdelete = false order by createTime desc")
    List<BigInteger> getUpVideoIds(BigInteger userid);
    //删除视频
    @Update("update video set isdelete = true where videoId=#{videoId}")
    void deleteById(BigInteger videoId);


    @Insert("insert into video (userId,title,description,path,createTime,likeCount,viewCount,commentId,tagId) values (#{userId},#{title},#{description},#{path},#{createTime},#{likeCount},#{viewCount},#{commentId},#{tagId})")
    @Options(useGeneratedKeys = true,keyProperty = "videoId",keyColumn = "videoId")
    void create(Video newVideo);

    @Select("select * from video where videoId=#{videoId} and isdelete = false")
    Video getById(BigInteger id);


    @Update("update video set title=#{title},description=#{description},url=#{url},upTime=#{upTime},playTime=#{playTime} where videoId=#{videoId}")
    Boolean update(Video newVideo);



    @Update("update video set likeCount=likeCount+1 where videoId=#{videoId}")
    void addLikeCount(BigInteger videoId);
    @Update("update video set likeCount=likeCount-1 where videoId=#{videoId}")
    void deLikeCount(BigInteger videoId);

    @Select("select userId from video where videoId = #{videoId} and isdelete = false")
    BigInteger getUserId(BigInteger videoId);

    @Select("""
    <script>
      SELECT videoId, tagId
      FROM   video
      WHERE  videoId IN
      <foreach collection="videoIds" item="id" open="(" separator="," close=")">
        #{id}
      </foreach>
    </script>
""")
    List<VideoTag> getTagIds(@Param("videoIds") List<BigInteger> videoIds);


    @Select("""
        SELECT  videoId
        FROM    videolikes
        GROUP BY videoId
        ORDER BY COUNT(videoId) DESC
        LIMIT   #{count}
        """)
    List<BigInteger> findTopNHotVideoIds(int count);


    @Select("SELECT videoId FROM video as t1 WHERE t1.videoId>=(RAND()*(SELECT MAX(videoId) FROM video)) LIMIT #{count}")
    List<BigInteger> getRandomVideos(int count);


    @Select("select * from video WHERE (title LIKE #{keyword} OR description LIKE #{keyword}) AND isdelete = 0 limit 20")
    List<Video> findVideosByKeyword(String keyword);
}
